package com.chijun.mform.process;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.MenuProvider;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

import com.chijun.mform.R;
import com.chijun.mform.full.mode.ModeFormFullApprovePerson;
import com.chijun.mform.process.view.ViewLoadApprovalControl;
import com.chijun.mform.process.view.ViewLoadFullFieldData;
import com.chijun.mform.process.view.ViewLoadFullProcessData;
import com.chijun.mform.process.view.MainViewLoadProcessAllData;
import com.chijun.mform.test.CreateFormServiceImpl;
import com.example.ccj.b.a.BaseActivity;
import com.example.ccj.b.c.draw.TopBar;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.ToastUtils;
import com.example.ccj.u.server.MyObserver;
import com.example.ccj.u.server.Result;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoadFullFormDataActivity extends BaseActivity implements IHandlerLoadFormFull, MainViewLoadProcessAllData.IProcessViewApproveOver{

    ViewLoadFullFieldData loadFullDataView;
    ViewLoadFullProcessData loadFullProcessDataView;
    ViewLoadApprovalControl loadApprovalView;
    TopBar topBar;
    @Override
    public void initData(@Nullable Bundle bundle) {
        if (bundle != null) {
            String fullData = bundle.getString("full_data");
            String fullId = bundle.getString("form_full_id");

            MainViewLoadProcessAllData loadProcessAllData = findViewById(R.id.load_process_all_data_view);

            if (fullData != null) {
//                jieXiData(fullData);
                loadProcessAllData
                        .setFormFullData(fullData,this)
                        .setUploadOverListener(this)
                        .builder();

            } else if (fullId != null){
                loadProcessAllData
                        .setFormFullId(fullId,this)
                        .setUploadOverListener(this).builder();
            } else {
                LogUtils.d("展示表单数据必须传入数据id（form_full_id）或者表单数据（full_data）");
            }
        }
    }

    private void jieXiData(String data){
        LogUtils.d("加载");
        showLoadingDialog();

        Observable.create((ObservableOnSubscribe<ModeServerFullData>) e -> {
            ModeServerFullData serverFullData = ModeServerFullData.analysis(data);
            e.onNext(serverFullData);
        }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<ModeServerFullData>() {
                    @Override
                    public void onNext(ModeServerFullData serverFullData) {
                        if (serverFullData != null) {
                            dismissLoadingDialog();
                            loadData(serverFullData);
                        } else {
                            dismissLoadingDialog();
                            ToastUtils.showLong("未获取到数据");
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoadingDialog();
                        super.onError(e);
                    }
                });
    }


    private void loadData(ModeServerFullData serverFullData){
        topBar.setTitle(String.valueOf(serverFullData.launchedForm.formId));
        //先加载审批流程
        loadFullProcessDataView.setData(serverFullData, () -> {
            //审批流程加载完毕加载字段数据
            loadFullDataView.setData(serverFullData,this);
            loadApprovalView.setData(serverFullData,this,new IWritePingJiaCallBack() {
                @Override
                public void onSuccessPl(long a, ModeFormFullApprovePerson formFullApprovePerson) {
                    loadFullProcessDataView.refreshAddData(a,formFullApprovePerson);
                }

                @Override
                public void onSuccessSp() {
                    finish();
                }
            });
        });

    }


    @Override
    public void initView(@Nullable Bundle bundle, @Nullable View view) {
//        loadFullDataView = findViewById(R.id.activity_load_full_form_data_filed_data);
//        loadFullProcessDataView = findViewById(R.id.activity_load_full_form_data_process_data);
//        loadApprovalView = findViewById(R.id.activity_load_process_mode);
    }

    @Override
    public int initContentView() {
        topBar = showTopBar("表单");
        return R.layout.activity_load_full_form_data;
    }

    @Override
    public void addMenuProvider(@NonNull MenuProvider provider, @NonNull LifecycleOwner owner, @NonNull Lifecycle.State state) {

    }

    @Override
    public void onOver() {
        finish();
    }


    /**
     * 上传评论
     * @param approvePerson 评论
     * @param waitUploadCompleteListener 回调给调用类
     */
    @Override
    public void uploadPingLun(ModeFormFullApprovePerson approvePerson, IWaitUploadCompleteListener waitUploadCompleteListener) {
        CreateFormServiceImpl.getInstance().formFullPingLun(approvePerson)
                .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<Result>() {
                    @Override
                    public void onNext(Result result) {
                        waitUploadCompleteListener.onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        waitUploadCompleteListener.onError(e.getMessage());
                        super.onError(e);
                    }
                });
    }

    /**
     *
     * @param approvePerson 转交人
     * @param formFullApprovePersonId 审批人员
     * @param waitUploadCompleteListener 转交完毕回调
     */
    @Override
    public void uploadZhuanJiao(ModeFormFullApprovePerson approvePerson, String formFullApprovePersonId, IWaitUploadCompleteListener waitUploadCompleteListener) {
        CreateFormServiceImpl.getInstance().formFullZhuanJiao(approvePerson,formFullApprovePersonId)
                .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<Result>() {
                    @Override
                    public void onNext(Result result) {
                        waitUploadCompleteListener.onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        waitUploadCompleteListener.onError(e.getMessage());
                        super.onError(e);
                    }
                });
    }

    /**
     *
     * @param approvePerson 审批人
     * @param waitUploadCompleteListener 审批结果上传后回调
     */
    @Override
    public void uploadShenPi(ModeFormFullApprovePerson approvePerson, IWaitUploadCompleteListener waitUploadCompleteListener) {
        CreateFormServiceImpl.getInstance().formFullUpdateApprove(approvePerson)
                .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<Result>() {
                    @Override
                    public void onNext(Result result) {
                        waitUploadCompleteListener.onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        waitUploadCompleteListener.onError(e.getMessage());
                        super.onError(e);
                    }
                });
    }

    /**
     *
     * @param formFullId 表单id
     * @param waitUploadCompleteListener 返回信息回调
     */
    @Override
    public void loadFullData(String formFullId, IWaitLoadCompleteResultDataListener waitUploadCompleteListener) {
        CreateFormServiceImpl.getInstance().loadFullData(formFullId)
                .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<Result>() {
                    @Override
                    public void onNext(Result result) {
                        waitUploadCompleteListener.onComplete(result.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        waitUploadCompleteListener.onError(e.getMessage());
                        super.onError(e);
                    }
                });
    }
}